<?php

namespace common\models\project;

use Yii;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "hxdc_project_area".
 *
 * @property int $id
 * @property int $merchant_id 商户id
 * @property string $area_name 区域名
 * @property string $ssq_area =arctype表ssq
 * @property int $status 状态0,1
 * @property int $create_time 创建时间时间戳
 * @property int $updated_at 更新时间
 */
class ProjectArea extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hxdc_project_area';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['merchant_id', 'ssq_area', 'create_time'], 'required'],
            [['status', 'create_time', 'updated_at'], 'integer'],
            [['area_name'], 'string', 'max' => 30],
            [['ssq_area'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'merchant_id'=> '商户id',
            'area_name' => '标题',
            'ssq_area' => '所属区域',
            'status' => '状态',
            'create_time' => '创建时间',
            'updated_at' => '最后更新时间',
        ];
    }
    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}
